//
//  EvaluationViewController.m
//  HTP
//
//  Created by admin on 15/11/4.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "EvaluationViewController.h"
#import "ClassCourseDetailViewController.h"
#import "PlaceholderTextView.h"
#import "ClassCourseViewModel.h"
@interface EvaluationViewController ()
@property (nonatomic,retain) UILabel    *courseSub;
@property (nonatomic,retain) UILabel    *teacherName;
@property (nonatomic,retain) UILabel    *dateLaebl;

@property (nonatomic,retain) UIView     *eveView;
@property (nonatomic,retain) PlaceholderTextView *eveluationDetail;

@property (nonatomic,retain) NSMutableArray *eveArray;//评价按钮存放数组
@property (nonatomic,assign) NSInteger      accessCount;
@end

@implementation EvaluationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initView];
//    [self.view setBackgroundColor:RGBCOLOR(240, 240, 240, 1)];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [self addCustomBackBase];
    [self setNavTitle:@"评价"];
    [self addCustomRightWithAction:@selector(sureAction) title:@"确定" normalImg:nil selectImg:nil];
    [self setCourseData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillHideNotification];
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillShowNotification];
}
//TODO:绘制界面
/**
 *  绘制界面-- yunhe
 */
-(void)_initView{
    UIView *superView = (UIView *)self.view;
    
    _courseSub = [self getNormolLabel];
    _courseSub.font = UIFONTSYS(16);
    _courseSub.textAlignment = _TEXT_ALIGN_LEFT;
    [self.view addSubview:_courseSub];
    [_courseSub makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.top.equalTo(self.view).offset(10);
    }];
    
    _teacherName = [self getNormolLabel];
    _teacherName.font = UIFONTSYS(16);
    _teacherName.textAlignment = _TEXT_ALIGN_LEFT;
    [self.view addSubview:_teacherName];
    [_teacherName makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.top.equalTo(_courseSub.bottom).offset(10);
    }];
    
    _dateLaebl = [self getNormolLabel];
    _dateLaebl.font = UIFONTSYS(16);
    _dateLaebl.textAlignment = _TEXT_ALIGN_LEFT;
    [self.view addSubview:_dateLaebl];
    [_dateLaebl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.top.equalTo(_teacherName.bottom).offset(10);
    }];
    
    _eveView = [[UIView alloc] init];
    [superView addSubview:_eveView];
    [_eveView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_dateLaebl);
        make.top.equalTo(_dateLaebl.bottom).offset(10);
        make.height.equalTo(20);
    }];
    [self _initEveView];
    UIImageView *line_h = [[UIImageView alloc] init];
    [line_h setImage:[UIImage imageNamed:@"line_h"]];
    [superView addSubview:line_h];
    [line_h makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(superView);
        make.top.equalTo(_eveView.bottom).offset(40);
        make.height.equalTo(1);
    }];
    
    _eveluationDetail = [[PlaceholderTextView alloc] init];
    _eveluationDetail.font = [UIFont systemFontOfSize:13];
    [_eveluationDetail setBackgroundColor:RGBCOLOR(243, 243, 243, 1)];
    _eveluationDetail.placeholderColor = [UIColor grayColor];
    _eveluationDetail.textColor = RGBCOLOR(0, 0, 0, 0.7);
    [_eveluationDetail setPlaceholder:@"请输入评价.."];
    _eveluationDetail.layer.borderColor = UIColor.whiteColor.CGColor;
    _eveluationDetail.layer.borderWidth = 1;
    _eveluationDetail.layer.cornerRadius = 6;
    _eveluationDetail.layer.masksToBounds = YES;
//    [_eveluationDetail setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_eveluationDetail];
}
-(void)_initEveView{
    _eveArray = [NSMutableArray array];
    UILabel *pjLabel = [self getNormolLabel];
    pjLabel.textAlignment = _TEXT_ALIGN_LEFT;
    pjLabel.text= @"评价: ";
    pjLabel.font = UIFONTSYS(16);
    CGSize size = [pjLabel.text textSizeWithFont:UIFONTSYS(16) constrainedSize:CGSizeMake(ScreenWidth, 20)];
    [pjLabel setFrame:CGRectMake(0, 0, size.width, 20)];
    [_eveView addSubview:pjLabel];
    
    for (int i = 0; i < 5; i ++) {
        UIButton *xxBtn = [[UIButton alloc] initWithFrame:CGRectMake(size.width + 5 + (25)*i, 0, 20, 20)];
        [xxBtn setBackgroundImage:[UIImage imageNamed:@"unstart"] forState:UIControlStateNormal];
        [xxBtn setBackgroundImage:[UIImage imageNamed:@"star"] forState:UIControlStateSelected];
        xxBtn.tag = i;
        [_eveView addSubview:xxBtn];
        [_eveArray addObject:xxBtn];
        [xxBtn addTarget:self action:@selector(eveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i<=2) {
            xxBtn.selected = YES;
        }
        
    }
    _accessCount = 2;
    
}
/**
 *  获取数据后设置数据
 */
-(void)setCourseData{
   
    [_courseSub setText:[NSString stringWithFormat:@"课程: %@%@",_courseRecordDetail.gradeName,_courseRecordDetail.courseName]];
    [_teacherName setText:[NSString stringWithFormat:@"教师： %@",_courseRecordDetail.teacherName]];
    [_dateLaebl setText: [NSString stringWithFormat:@"%@ %@ %@",_courseRecordDetail.cgcDate,_courseRecordDetail.crStartTime,_courseRecordDetail.crEndTime]];
    [self.view layoutIfNeeded];
    [_eveluationDetail setFrame:CGRectMake(10,CGRectGetMaxY(_eveView.frame) + 60, ScreenWidth - 20, ScreenHeihgt - CGRectGetMaxY(_eveView.frame) - 60 -NavHe-10)];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark - keyboard
-(void) keyboardWillHiden:(NSNotification *)notification{
    [UIView animateWithDuration:0.5 animations:^{
        [_eveluationDetail setFrame:CGRectMake(10,CGRectGetMaxY(_eveView.frame) + 60, ScreenWidth - 20, ScreenHeihgt - CGRectGetMaxY(_eveView.frame) - 60 -NavHe-10)];
    }];
}
-(void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat keyboardOffset = CGRectGetHeight(keyboardRect);
    [UIView animateWithDuration:animationDuration animations:^{
         [_eveluationDetail setFrame:CGRectMake(10,25, ScreenWidth - 20, ScreenHeihgt - NavHe - 25 - keyboardOffset - 20)];
    }];
}

//TODO:action
/**
 *  星星按钮事件
 *
 *  @param sender
 */
-(void)eveBtnClick:(id)sender{
    UIButton *btn = (UIButton *)sender;
    NSInteger tag = btn.tag;
    _accessCount = btn.tag;
    [_eveArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *selectBtn = (UIButton *)obj;
        if (tag >= selectBtn.tag) {
            selectBtn.selected = YES;
        }else{
            selectBtn.selected = NO;
        }
    }];
}
/**
 *  右键确定递交
 */
-(void)sureAction{
    if ([self vaildData]) {
        ClassCourseViewModel *classCourseVM = [[ClassCourseViewModel alloc] init];
        [classCourseVM setBlockWithReturnBlock:^(id returnValue) {
            if (self.delegate) {
                [self.delegate finishEvaluation:[NSNumber numberWithInteger:(_accessCount + 1)] detail:_eveluationDetail.text];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:CHANGECOURSERECORD object:nil];
            [self backToQM];
        } WithErrorBlock:^(id errorCode) {
            [self showSVProgressFailed:INTERFACE_ERROR_CHIN];
        } WithFailureBlock:^{
             [self showSVProgressFailed:INTERFACE_FAILURE_CHIN];
        }];
        [classCourseVM addCourseClassAccess:_eveluationDetail.text == nil ? @"":_eveluationDetail.text andAccess:[NSString stringWithFormat:@"%ld",(long)(_accessCount+1)] withCourseReordId:[NSString stringWithFormat:@"%@",_courseRecordDetail.crId]];
    }
    
    
}
/**
 *  校验数据
 *
 *  @return
 */
-(BOOL)vaildData{
    if (_accessCount < 2 && _eveluationDetail.text.length == 0) {
        [self showSVProgressFailed:@"评级过低,必须评价"];
        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
